home comics writing pictures archive about

tictactoe.c

Language: C
Last Modified: 2020-06-27 1:58:34 PM UTC
File Size: 10987 bytes
http://www.penguinstew.ca/example/tictactoe/tictactoe.c
#include <windows.h>
#define SIZE 3
#define NONE 0
#define X 1
#define O 2
#define CAT 3
#define BOARD_SIZE 100*SIZE
#define STATUS_SIZE 100
#define PADDING 10
#define WHITE RGB(255,255,255)
#define BLACK RGB(0,0,0)
#define GREY RGB(200,200,200)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int checkBoard(int board[SIZE][SIZE]);
void drawCross(HDC hdc);
void drawSquare(HDC hdc, int x, int y, int player, COLORREF colour);
void drawStatus(HDC hdc, int gameOver, int currentTurn, int score[3]);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
static TCHAR szAppName[] = TEXT("tictactoe");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
//populate the window class struct
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
//Register the class
if(!RegisterClass(&wndclass)){
MessageBox(NULL, TEXT("Class registration failed"),
szAppName, MB_ICONERROR);
return 0;
}
//Create and show the window
hwnd = CreateWindow(szAppName, TEXT("Tic Tac Toe"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER,
CW_USEDEFAULT, CW_USEDEFAULT,
(BOARD_SIZE + PADDING*2 + 6), (BOARD_SIZE + STATUS_SIZE + PADDING*3 + 26),
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
//Message loop
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
static int cState[SIZE][SIZE];
static int cLastPositionX;
static int cLastPositionY;
static int currentTurn;
static int score[3];
static int gameOver;
static int cxBlock, cyBlock;
HDC hdc;
int x, y;
PAINTSTRUCT ps;
switch(message) {
case WM_CREATE:
//Get the square size
cxBlock = BOARD_SIZE / SIZE;
cyBlock = BOARD_SIZE / SIZE;
//Setup the starting position
cLastPositionX = -1;
cLastPositionY = -1;
//Setup the starting player
currentTurn = X;
//Setup the starting score
score[NONE] = 0;
score[X] = 0;
score[O] = 0;
gameOver = NONE;
//Set all squares to be empty
for(x = 0 ; x < SIZE ; x++) {
for(y = 0 ; y < SIZE ; y++) {
cState[x][y] = NONE;
}
}
return 0;
case WM_LBUTTONDOWN:
//Find the square clicked on
x = (LOWORD(lParam)-PADDING) / cxBlock;
y = (HIWORD(lParam)-PADDING) / cyBlock;
//Make sure square is valid6
x = min(x,SIZE-1);
y = min(y,SIZE-1);
hdc = GetDC(hwnd);
//If the game isn't over
if(gameOver == NONE) {
//If the last position is not set
if(cState[cLastPositionX][cLastPositionY] == NONE) {
//Clear the last position
drawSquare(hdc,cLastPositionX,cLastPositionY,currentTurn,WHITE);
}
//If the current position is not set
if(cState[x][y] == NONE) {
//Set the current position to the current player
drawSquare(hdc,x,y,currentTurn,BLACK);
cState[x][y] = currentTurn;
//Check if the game is over
gameOver = checkBoard(cState);
//Toggle the current turn
if(currentTurn == X){
currentTurn = O;
}else {
currentTurn = X;
}
//If the game is over increase the score of the winner
switch (gameOver) {
case X:
score[X]++;
break;
case O:
score[O]++;
break;
case CAT:
score[NONE]++;
break;
}
//Update the status message
drawStatus(hdc,gameOver,currentTurn,score);
}
} else {
//If the game is over reset the board
gameOver = NONE;
for(x = 0 ; x < SIZE ; x++) {
for(y = 0 ; y < SIZE ; y++) {
cState[x][y] = NONE;
}
}
cLastPositionX = -1;
cLastPositionY = -1;
currentTurn = X;
//Redraw
InvalidateRect(hwnd,NULL, TRUE);
}
ReleaseDC(hwnd,hdc);
return 0;
case WM_MOUSEMOVE:
x = (LOWORD(lParam)-PADDING) / cxBlock;
y = (HIWORD(lParam)-PADDING) / cyBlock;
x = min(x,SIZE-1);
y = min(y,SIZE-1);
hdc = GetDC(hwnd);
//If the game isn't over
if(gameOver == NONE) {
//If the position has changed square
if(cLastPositionX != x || cLastPositionY != y) {
//If the last position is not set
if(cState[cLastPositionX][cLastPositionY] == NONE) {
//Clear the last position
drawSquare(hdc,cLastPositionX,cLastPositionY,currentTurn,WHITE);
}
//If the current position is not set
if(cState[x][y] == NONE) {
//Display the ghost
drawSquare(hdc,x,y,currentTurn,GREY);
}
}
}
cLastPositionX = x;
cLastPositionY = y;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//draw cross
drawCross(hdc);
//Loop through the grid and display and set squares
for(x = 0 ; x < SIZE ; x++) {
for(y = 0 ; y < SIZE ; y++) {
if(cState[x][y] != NONE) {
drawSquare(hdc,x,y,cState[x][y],BLACK);
}
}
}
//If the last position is not set
if(cState[cLastPositionX][cLastPositionY] == NONE) {
//Display the ghost
drawSquare(hdc,cLastPositionX,cLastPositionY,currentTurn,GREY);
}
//Draw the status
drawStatus(hdc,gameOver, currentTurn, score);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//Draw in a square
void drawSquare(HDC hdc, int x, int y, int player, COLORREF colour) {
//Get the square size
int cxBlock = BOARD_SIZE / SIZE;
int cyBlock = BOARD_SIZE / SIZE;
RECT rc;
TCHAR szBuffer[50] = TEXT("");
//Create a rect for the square
rc.left = x*cxBlock + PADDING;
rc.top = y*cyBlock + PADDING;;
rc.right = (x+1)*cxBlock + PADDING;;
rc.bottom = (y+1)*cyBlock + PADDING;
//Create a new font and select it
SelectObject(hdc, CreateFont(90, 0, 0, FW_BOLD, 0, 0, 0, 0,
0, 0, 0, 0, DEFAULT_PITCH, NULL));
//Load the text based on the player
if(player == X) {
wsprintf(szBuffer, TEXT("X"));
} else {
wsprintf(szBuffer, TEXT("O"));
}
//Set the colour
SetTextColor(hdc, colour);
//Draw the X or O in the given colour in the square
DrawText(hdc, szBuffer,-1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//Delete the font
DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
}
//Draw the game board
void drawCross(HDC hdc) {
//Get the square size
int cxBlock = BOARD_SIZE / SIZE;
int cyBlock = BOARD_SIZE / SIZE;
int i;
SelectObject(hdc, CreatePen(PS_SOLID, 5, 0));
//Virtical
for(i = 1 ; i < SIZE ; i++) {
MoveToEx(hdc,((cxBlock*i)+PADDING),PADDING,NULL);
LineTo(hdc,((cxBlock*i)+PADDING),(BOARD_SIZE+PADDING));
}
//Horizontal
for(i = 1 ; i < SIZE ; i++) {
MoveToEx(hdc,PADDING,((cyBlock*i)+PADDING),NULL);
LineTo(hdc,(BOARD_SIZE+PADDING),((cyBlock*i)+PADDING));
}
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
}
//Draw the status section
void drawStatus(HDC hdc, int gameOver, int currentTurn, int score[3]) {
//Get the square size
int cxBlock = BOARD_SIZE / SIZE;
int cyBlock = BOARD_SIZE / SIZE;
RECT rc;
TCHAR szBuffer[50] = TEXT("");
SelectObject(hdc, CreatePen(PS_SOLID, 5, 0));
//Draw the status box
Rectangle(hdc,PADDING,(BOARD_SIZE+PADDING*2),(BOARD_SIZE+PADDING),(BOARD_SIZE+PADDING*2+STATUS_SIZE));
//Create a new font and select it
SelectObject(hdc, CreateFont(30, 0, 0, FW_BOLD, 0, 0, 0, 0,
0, 0, 0, 0, DEFAULT_PITCH, NULL));
//Determine status message from the current state
switch (gameOver) {
case NONE:
if(currentTurn == X) {
wsprintf(szBuffer, TEXT("It is X's Turn"));
} else {
wsprintf(szBuffer, TEXT("It is O's Turn"));
}
break;
case X:
wsprintf(szBuffer, TEXT("X HAS WON!"));
break;
case O:
wsprintf(szBuffer, TEXT("O HAS WON!"));
break;
case CAT:
wsprintf(szBuffer, TEXT("CAT HAS WON!"));
break;
}
SetTextColor(hdc, BLACK);
rc.left = PADDING*2;
rc.top = (BOARD_SIZE+PADDING*3);
rc.right = BOARD_SIZE;
if(gameOver == NONE) {
rc.bottom = BOARD_SIZE + STATUS_SIZE + PADDING;
} else {
rc.bottom = BOARD_SIZE + (STATUS_SIZE/2) + PADDING;
}
DrawText(hdc, szBuffer,-1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
if(gameOver != NONE) {
rc.top = BOARD_SIZE + (STATUS_SIZE/2) + PADDING;
rc.bottom = BOARD_SIZE + STATUS_SIZE + PADDING;
wsprintf(szBuffer, TEXT("CAT - %i, X - %i, O - %i"), score[NONE], score[X], score[O]);
DrawText(hdc, szBuffer,-1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
}
int checkBoard(int board[SIZE][SIZE]) {
int current = -1;
int x;
int y;
//Check columns
for(x = 0 ; x < SIZE ; x++) {
for(y = 0 ; y < SIZE ; y++) {
if(y == 0) {
current = board[x][y];
} else if(current != board[x][y]) {
current = -1;
break;
}
}
if(current > 0) {
return current;
}
}
//Check rows
for(y = 0 ; y < SIZE ; y++) {
for(x = 0 ; x < SIZE ; x++) {
if(x == 0) {
current = board[x][y];
} else if(current != board[x][y]) {
current = -1;
break;
}
}
if(current > 0) {
return current;
}
}
//Check diagonal
for(y = 0 ; y < SIZE ; y++) {
if(y == 0) {
current = board[y][y];
} else if(current != board[y][y]) {
current = -1;
break;
}
}
if(current > 0) {
return current;
}
for(y = 0 ; y < SIZE ; y++) {
if(y == 0) {
current = board[y][SIZE-y-1];
} else if(current != board[y][SIZE-y-1]) {
current = -1;
break;
}
}
if(current > 0) {
return current;
}
//Check cat
for(x = 0 ; x < SIZE ; x++) {
for(y = 0 ; y < SIZE ; y++) {
if(board[x][y] == NONE) {
return NONE;
}
}
}
return CAT;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432